using System;
using System.Collections.Generic;
using System.Text;

namespace Slf.Samples.SetNullLogger
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.ForegroundColor = ConsoleColor.Green;
      string msg = "This sample illustrates the use of NullLogger, which basically just discards " +
                   "all log messages it receives.\n" +
                   "NullLogger is active per default (as long as no other logger is configured), " +
                   "but you can also set it explicitly in order to stop logging at runtime.\n\n\n";
      Console.Out.WriteLine(msg);
      Console.ResetColor();


      //If no logger is configured, the service automatically returns the NullLogger.
      //Accordingly, the code here doesn't do anything:
      LogMessage("This message will not appear");

      //write the type of the logger that is returned from LoggerService to console
      WriteCurrentLoggerTypeToConsole();

      //now assign a logger
      LoggerService.SetLogger(new ConsoleLogger());
      LogMessage("ConsoleLogger was plugged in. This message appears in the console.");
      
      
      //explicitly revert to the NullLogger (you get it via NullLogger.Instance)
      LoggerService.SetLogger(NullLogger.Instance);
      //this message will be discarded again
      LogMessage("Another message that will be discarded.");

      
      //You can also just set the default logger to null in order to fall back to the NullLogger:
      LoggerService.SetLogger(new ConsoleLogger());
      LoggerService.SetLogger(null);

      //write the type of the logger that is returned from LoggerService to console
      WriteCurrentLoggerTypeToConsole();
      
      Console.ReadLine();
    }


    private static void LogMessage(string msg)
    {
      ILogger logger = LoggerService.GetLogger();
      logger.Info(msg);
    }


    private static void WriteCurrentLoggerTypeToConsole()
    {
      ILogger logger = LoggerService.GetLogger();
      string msg = "Got this logger back from LoggerService: " + logger.GetType().FullName;
      Console.Out.WriteLine(msg);
    }
  }
}